﻿using System;
using System.Windows.Forms;

namespace site_navigator
{
   public partial class Form1 : Form
    {
        private bool modified = false;
        private int item_number = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //检查是否存在配置文件，不存在则创建一个
            System.IO.FileInfo fi = new System.IO.FileInfo("config.xml");
            if (fi.Exists == false)
            {
                System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter("config.xml", System.Text.Encoding.UTF8);

                writer.WriteStartDocument();
                writer.WriteStartElement("configure");

                writer.WriteElementString("timer1", "24");
                writer.WriteElementString("timer2", "10");

                writer.WriteStartElement("urls");

                string[] urls = { "http://scmy.pw", "https://www.2345.com/?k1966203","http://92ip.top","http://92ip.com","http://zybsoft.com" };
                foreach (string url in urls)
                {
                    writer.WriteElementString("url", url);
                }
                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteEndDocument();
                writer.Close();
            }

            //从配置文件加载配置数据
            try
            {
                System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                xmlDocument.Load("config.xml");
                System.Xml.XmlNodeList xnl = xmlDocument.SelectSingleNode("configure").ChildNodes;
                foreach (System.Xml.XmlNode xn in xnl)
                {
                    if (xn.Name == "timer1")
                    {
                        tb_timer1.Text = xn.InnerText;

                    }
                    if (xn.Name == "timer2")
                    {
                        tb_timer2.Text = xn.InnerText;

                    }
                    if (xn.Name == "urls")
                    {
                        System.Xml.XmlNodeList xn2 = xn.SelectNodes("url");
                        foreach (System.Xml.XmlNode xn3 in xn2)
                        {
                            listView1.Items.Add(xn3.InnerText);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

          

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text!="")
            { 
            listView1.Items.Add(textBox1.Text);
            listView1.Refresh();
            modified = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)  //save
        {
            try
            {
                System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter("config.xml", System.Text.Encoding.UTF8);

                writer.WriteStartDocument();
                writer.WriteStartElement("configure");

                writer.WriteElementString("timer1", tb_timer1.Text);
                writer.WriteElementString("timer2", tb_timer2.Text);

                    writer.WriteStartElement("urls");
                   for (int i=0;i<listView1.Items.Count;i++)
                   {
                      writer.WriteElementString("url", listView1.Items[i].Text);
                    }
                    writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteEndDocument();
                writer.Close();

                 MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                 MessageBox.Show("保存失败!");
            }



            
          
         
        }

      

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count>0)  listView1.SelectedItems[0].Remove();
            
            modified = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (modified)
            {
                DialogResult dr = MessageBox.Show("是否要进行保存？","退出确认",MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    button2_Click(null, null);
                }
                else
                {

                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1_Tick(null, null);
            timer1.Interval = Convert.ToInt16(tb_timer2.Text) * 1000;
            timer1.Enabled = true;
            timer2.Interval = Convert.ToInt16(tb_timer1.Text) * 60000 * 60;
            timer2.Enabled = true;
        }

        private void visit()
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tb_url.Text = listView1.Items[item_number].Text;
            listView1.Items[item_number].Selected = true;
            try {     webBrowser1.Navigate(tb_url.Text);} catch (Exception ex){
              //  webBrowser1.DocumentText=ex.Message;
            }
        
            item_number++;
            if (item_number == listView1.Items.Count)
            {
                item_number = 0;
                timer1.Enabled = false;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void btn_url_go_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(tb_url.Text);
        }

        private void tb_url_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)   //还可以使用 Keys.F 等　检测F键是否按下
            {
                webBrowser1.Navigate(tb_url.Text);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // tb_url.Text = listView1.Items[ listView1.SelectedIndices[0]].Text;
           // webBrowser1.Navigate(tb_url.Text);
        }

        private void webBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
